package com.xiaoyu.cloudmusic.utils;

import com.xiaoyu.cloudmusic.pojo.CommentInfo;
import com.xiaoyu.cloudmusic.pojo.UserInfo;

import java.util.List;

public class CommentUtil {
//    递归封装评论和回复
    public static List<CommentInfo> getAllComment(List<CommentInfo> firstCommentNodes,List<CommentInfo> replyNodes){
        while (!replyNodes.isEmpty()){
            int size = replyNodes.size();
            for (int i = 0; i < size; i++) {
                if(CommentUtil.addReplyNode(firstCommentNodes,replyNodes.get(i))){
                    replyNodes.remove(i);
                    i--;
                    size--;
                }
            }
        }
        return firstCommentNodes;
    }
//   递归封装回复
    public static boolean addReplyNode(List<CommentInfo> list,CommentInfo reply){
        for(CommentInfo node : list){
            if(node.getId() == reply.getLast_id()){
                node.getResponse().add(reply);
                return true;
            }else {
                if(!node.getResponse().isEmpty()){
                    if(CommentUtil.addReplyNode(node.getResponse(),reply)){
                        return true;
                    }
                }
            }
        }
        return false;
    }
//   封装数据库数据
    public static CommentInfo createComment(String uuid,String message,UserInfo userInfo,int listId,String format){
        CommentInfo commentInfo = new CommentInfo();
//       封装数据库数据
        commentInfo.setUuid(uuid);
        commentInfo.setNickname(userInfo.getNickname());
        commentInfo.setPicUrl(userInfo.getPhotoPath());
        commentInfo.setTime(DataUtil.getFormatter(format));
        commentInfo.setMessage(message);
        commentInfo.setListId(listId);
        return commentInfo;
    }
    public static CommentInfo createComment(String uuid,String message,UserInfo userInfo,int listId,int lastId,String format){
        CommentInfo commentInfo = new CommentInfo();
//       封装数据库数据
        commentInfo.setUuid(uuid);
        commentInfo.setNickname(userInfo.getNickname());
        commentInfo.setPicUrl(userInfo.getPhotoPath());
        commentInfo.setTime(DataUtil.getFormatter(format));
        commentInfo.setMessage(message);
        commentInfo.setListId(listId);
        commentInfo.setLast_id(lastId);
        return commentInfo;
    }
//   封装点赞
    public static void encapsulateSupport(List<Integer> ids, List<CommentInfo> commentInfoList){
        for (CommentInfo commentInfo : commentInfoList) {
            commentInfo.setLike(ids.contains(commentInfo.getId()));
        }
    }
}
